'''
Created on Dec 12, 2011

@author: Box II
'''

import matplotlib.pyplot as plt
import matplotlib.colors as clv
import matplotlib.mlab as mlb
import math as m
import htmlAggregator as ha
from mpl_toolkits.basemap import Basemap
import numpy as np
from pylab import load, title, show
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas


def convertToDaysAfter2004(yearmonthdayStr):
    yeardiff=0
    monthdiff=0
    dayDiff=0
    
    if(yearmonthdayStr.find("/") > -1):
        attr = yearmonthdayStr.split("/")
        yeardiff = int(attr[2])-2004
        monthdiff = int(attr[0])
        dayDiff = int(attr[1])
    else:
        attr = yearmonthdayStr.split("-")
        yeardiff = int(attr[0])-2004
        monthdiff = int(attr[1])
        dayDiff = int(attr[2])

    
    result = (yeardiff*365)+(monthdiff*31)+dayDiff
    return result

def readFile(path,type):
    infile= open(path)
    strArray=[]
    cto=0
    for line in infile:
        cto=cto+1
        if cto>1:             
            splitted=line.split(",")
            splitted[0]=convertToDaysAfter2004(splitted[0])
            if(type==1):
               for i in range(0,len(splitted)-1):
                   splitted[i]=int(splitted[i])
            if(type==2):
               for i in range(9,len(splitted)-1):  
                   splitted[i]=int(splitted[i])
            strArray.append(splitted)
    
    return strArray


dataline=[]
infile= open("reducedData.csv")
datavalSIGACT=11
dataval1SIGACT=9
runnerSIGACT=0
runner1SIGACT=0
timeSIGACT=[]
dataSIGACT=[]
data1SIGACT=[]
queryYR=[]
for i in range(0,6):
    queryYR.append(([],[],[],[],[],[],[],[],[],[],[],[]))

ctoSIGACT=0
for line in infile:
    ctoSIGACT=ctoSIGACT+1
    if ctoSIGACT==1:
        dataline=line.split(",")
    if ctoSIGACT>1:             
        splitted=line.split(",")
        myvar=splitted[0]
        myvar=myvar.split("-")
        focus=int(splitted[datavalSIGACT])
        try:
            queryYR[int(myvar[0])-2004][int(myvar[1])-1].append(splitted)
            
            if int(myvar[1])-1 == 5:
                im=1
        except:
            print myvar
        

#map.bluemarble()


print "Enemy Dead: "+str(runnerSIGACT)
print "Civilian Dead: "+str(runner1SIGACT)
monthstats=[]
datStats=[]
dat1Stats=[]
for r in range(0,len(queryYR)):
    print "Looking at"+str(r)
    print "Recs:"+str(len(queryYR[r]))
    for i in range(0,len(queryYR[r])):
        mylats=[]
        mylons=[]
        Z=[]
        if (r==0 and i==4) or (r==5 and i==2):
            print "Discarding."
        else:
            for t in range(0,len(queryYR[r][i])):
                if queryYR[r][i][t][3] != "" and queryYR[r][i][t][4] != "":
                    mylats.append(float(queryYR[r][i][t][3]))
                    mylons.append(float(queryYR[r][i][t][4]))
                    Z.append(int(queryYR[r][i][t][11]))
            plt.clf()
            map = Basemap(resolution='i',projection='mill',llcrnrlon=38.73,llcrnrlat=27.48,urcrnrlon=47.99,urcrnrlat=37.84)
            map.fillcontinents(color='white',lake_color='white',zorder=0)
            #map.drawcoastlines()
            map.drawcountries()
            map.drawstates()
            #map.drawmapboundary() # draw a line around the map region
            map.drawparallels(np.arange(-80.,81.,20.),labels=[1,0,0,0])
            map.drawmeridians(np.arange(-180.,181.,20.),labels=[0,0,0,1])
            #map.bluemarble()
            #map.drawmapboundary(fill_color='blue') 
            x, y = map(mylons, mylats)
            print str(r)+"/"+str(i)
            plt.title("Month: "+str(2004+r)+"/"+str(i+1))
            z1=[]
            for d in range(0,len(Z)):
                z1.append(Z[d]*2)
            myplot= map.scatter(x,y,s=z1,c=Z,marker='o',cmap=plt.cm.gist_rainbow,linewidths=0,alpha=0.75)
            cbar=plt.colorbar()
            cbar.set_label("Civilian Casualties in Event")
            plt.savefig("imgs/"+str(2004+r)+"-"+str(i+1)+"CiviCasualties.png", dpi=110)
            #plt.show()

    



#plt.plot(monthstats,dat1Stats,label="Average Incident Count")


